#include "container.h"

#include <iostream>
#include <utility>
#include <QDebug>

Container::Container() {

}

Container::~Container()
{
    for (int i = 0; i < cont.size(); i++) {
        if (cont[i] != nullptr) {
            delete cont[i];
        }
    }
}

void Container::add(Shape* figure) {
    cont.push_back(figure);
}

void Container::del(int ind) {
    cont.erase(cont.begin() + ind);
}

void Container::printShapesToFile(const std::string &filename) {
    std::ofstream outputFile;
    outputFile.open(filename + ".txt");
   for (unsigned int i = 0; i < cont.size(); i++) {
        /*if ((cont[i])->type() == "R") {
            outputFile << "R ";
        }
        if (cont[i]->type() == "C") {
            outputFile << "C ";
        }
        if (cont[i]->type() == "S") {
            outputFile << "S ";
        }
        std::vector<double> v = cont[i]->getInfo();
        for (int j = 0; j < v.size(); j++) {
            outputFile << v[j] << ' ';
        }

        outputFile << "\n";
        */
        outputFile << cont[i]->type() << ' ' << cont[i]->x << ' ' << cont[i]->y << ' ' << cont[i]->rotat << '\n';
    }

    outputFile.close();
}

std::pair<int, int> getCoord(std::string s) {
    std::pair<int, int> coord;
    int i = 2;
    std::string tmp;
    while (s[i] >= '0' && s[i] <= '9') {
        tmp += s[i];
        i++;
    }
    coord.first = std::stoi(tmp);
    tmp = "";
    i++;
    while (i < s.size() && s[i] != ' ') {
        tmp += s[i];
        i++;
    }
    coord.second = std::stoi(tmp);
    return coord;
}

double getRotat(std::string s) {
    int i = s.size() - 1;
    std::string tmp = "";
    while (s[i] >= '0' && s[i] <= '9') {
        i--;
    }
    if (s[i] == '-') {
        tmp += '-';
    }
    i++;
    while (i < s.size()) {
        tmp += s[i];
        i++;
    }
    return std::stoi(tmp);
}

void Container::getDataFromFile(const std::string &path) {
    std::ifstream inputFile (path);
    if (inputFile.is_open()) {
        std::string line;
        while (getline(inputFile, line)) {

            if (line[0] == 'R') {
                Rectangle* r = new Rectangle();
                r->figType = 'R';
                std::pair<int, int> tmp = getCoord(line);
                r->x = tmp.first;
                r->y = tmp.second;
                r->rotat = getRotat(line);
                cont.push_back(r);
            }
            if (line[0] =='C') {
                Circle* c = new Circle();
                c->figType = 'C';
                std::pair<int, int> tmp = getCoord(line);
                c->x = tmp.first;
                c->y = tmp.second;
                c->rotat = getRotat(line);
                cont.push_back(c);
            }
            if (line[0] == 'S') {
                Square* s = new Square();
                std::pair<int, int> tmp = getCoord(line);
                s->figType = 'S';
                s->x = tmp.first;
                s->y = tmp.second;
                s->rotat = getRotat(line);
                cont.push_back(s);
            }
        }
        inputFile.close();
    }
}

int Container::getSize()
{
    return cont.size();
}

Shape *Container::getShapeAt(int ind)
{
    if (ind < cont.size()) {
        return cont.at(ind);
    } else {
        qDebug() << "Warning! At function container::getShapeAt() index of boundary exception";
        return nullptr;
    }
}




